package dev.android.yeqh.tvannounce;

import java.util.List;
import net.youmi.android.AdManager;
import dev.android.yeqh.tvannounce.adapters.AreaListAdapter;
import dev.android.yeqh.tvannounce.common.Constants;
import dev.android.yeqh.tvannounce.common.NetTask;
import dev.android.yeqh.tvannounce.common.Utils;
import dev.android.yeqh.tvannounce.model.Area;
import dev.android.yeqh.tvannounce.model.NetTaskParam;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TVAnnounceActivity extends BaseActivity {
	ListView lvAreas = null;
	AreaListAdapter adapter = null;
	
	OnItemClickListener listener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			try {
				if (arg2 == 0) {
					Intent intent = new Intent(TVAnnounceActivity.this, SubChannelsActivity.class);
					intent.putExtra("station_id", "CCTV");
					TVAnnounceActivity.this.startActivity(intent);
				} else if (arg2 == 1) {
					Intent intent = new Intent(TVAnnounceActivity.this, SubChannelsActivity.class);
					intent.putExtra("station_id", "SATVLIST");
					TVAnnounceActivity.this.startActivity(intent);
				} else {
					Intent intent = new Intent(TVAnnounceActivity.this, ChannelsActivity.class);
					intent.putExtra("area_name", adapter.getAreaName(arg2));
					TVAnnounceActivity.this.startActivity(intent);
				}
			} catch (Exception e) {
				Utils.showMessage(TVAnnounceActivity.this, e);
			}
		}
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //第一个参数为您的应用发布Id
  		//第二个参数为您的应用密码
  		//第三个参数是请求广告的间隔，有效的设置值为30至200，单位为秒
  		//第四个参数是设置测试模式，设置为true时，可以获取测试广告，正式发布请设置此参数为false
  		AdManager.init(this, "5bebebbb02d4df2a", "6336363909c4a182", 30, false);
        
        lvAreas = (ListView)findViewById(R.id.main_areas);
        
        try {
        	new NetTask(this).execute(new NetTaskParam(Constants.NET_TASK_GET_AREAS, null));
        	adapter = new AreaListAdapter(null, this, R.layout.item_area);

        	lvAreas.setAdapter(adapter);
        	lvAreas.setOnItemClickListener(listener);
        } catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    @SuppressWarnings("unchecked")
	@Override
    public void updateUI(Object result) {
    	adapter.setAreas((List<Area>)result);
    	adapter.notifyDataSetChanged();
    }
}